package it.tukano.swingdeco.text;

import it.tukano.text.TextFormat;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

public class DecimalDocument extends PlainDocument {

    /* default serial version uid*/
    private static final long serialVersionUID = 0;

    /**
     * Instance initializer
     */
    public DecimalDocument() {
    }

    @Override
    public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
        str = TextFormat.getInstance().filterCharsInclusive(str, "0123456789.");
        if(hasDot() && str.contains(".")) {
            str = str.replace(".", "");
        }
        if(str.length() != 0) {
            super.insertString(offs, str, a);
        }
    }

    public boolean hasDot() throws BadLocationException {
        return getText(0, getLength()).contains(".");
    }
}
